      1  import java.awt.event.ActionEvent;

      2  import java.awt.event.ActionListener;

      3  import javax.swing.JButton;

      4  import javax.swing.JFrame;

      5  import javax.swing.JLabel;

      6  import javax.swing.JPanel;

      7

      8  /**

      9     This program displays the growth of an investment.

     10  */

     11  public class InvestmentViewer2

     12  {

     13     private static final int FRAME_WIDTH = 400;

     14     private static final int FRAME_HEIGHT = 100;

     15

     16     private static final double INTEREST_RATE = 10;

     17     private static final double INITIAL_BALANCE = 1000;

     18

     19     public static void main(String[] args)

     20     {

     21        JFrame frame = new JFrame();

     22

     23        // The button to trigger the calculation

     24        JButton button = new JButton("Add Interest");

     25

     26        // The application adds interest to this bank account

     27        final BankAccount account = new BankAccount(INITIAL_BALANCE);

     28

     29        // The label for displaying the results

     30        final JLabel label = new JLabel("balance: " + account.getBalance());

     31

     32        // The panel that holds the user interface components

     33        JPanel panel = new JPanel();

     34        panel.add(button);

     35        panel.add(label);

     36        frame.add(panel);

     37

     38        class AddInterestListener implements ActionListener

     39        {

     40           public void actionPerformed(ActionEvent event)

     41           {

     42              double interest = account.getBalance() * INTEREST_RATE / 100;

     43              account.deposit(interest);

     44              label.setText("balance: " + account.getBalance());

     45           }

     46        }

     47

     48        ActionListener listener = new AddInterestListener();

     49        button.addActionListener(listener);

     50

     51        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

     52        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     53        frame.setVisible(true);

     54     }

     55  }